package com.zhipu.balance.mobile.dao;

import android.text.TextUtils;

import com.raizlabs.android.dbflow.sql.language.SQLite;
import com.zhipu.balance.mobile.event.RecordCorn;
import com.zhipu.balance.mobile.event.RecordCorn_Table;
import com.zhipu.balance.mobile.event.RecordRice;
import com.zhipu.balance.mobile.event.RecordRice_Table;
import com.zhipu.balance.mobile.event.RecordWheat;
import com.zhipu.balance.mobile.event.RecordWheat_Table;

import java.util.List;

public class RecordCornDao {

    private RecordCornDao() {
    }

    private static final class SingletonHolder {
        private static final RecordCornDao INSTANCE = new RecordCornDao();
    }

    public static RecordCornDao getInstance() {
        return SingletonHolder.INSTANCE;
    }





    /**
     * 获取所有数据
     *
     * @return
     */
    public List<RecordCorn> getAllList() {
        return SQLite.select().from(RecordCorn.class).where().orderBy(RecordCorn_Table.recordCornId, true).queryList();
    }


    /**
     * 分页获取
     *
     * @param nowPage
     * @param uniqueId
     * @return
     */
    public List<RecordCorn> PagingFetch(int nowPage, String uniqueId) {
        if (TextUtils.isEmpty(uniqueId)) {
            return SQLite.select().from(RecordCorn.class).where().orderBy(RecordCorn_Table.recordCornId, false).limit(9).offset(nowPage).queryList();
        } else {
            return SQLite.select().from(RecordCorn.class).where(RecordCorn_Table.uniqueId.like("%" + uniqueId + "%")).orderBy(RecordCorn_Table.recordCornId, false).limit(9).offset(nowPage).queryList();
        }
    }

    /**
     * 根据编号查询
     * @param productCategoryText
     * @return
     */
    public RecordCorn QueryByNumber(String productCategoryText){
        RecordCorn mRecordCorn = SQLite.select().from(RecordCorn.class).where(RecordCorn_Table.uniqueId.eq(productCategoryText)).querySingle();
        if(null == mRecordCorn){
            mRecordCorn = new RecordCorn();
        }
        return mRecordCorn;
    }


    /**
     * 总条数
     * @return
     */
    public long TotalCount(String uniqueId){
        if(TextUtils.isEmpty(uniqueId)){
            return SQLite.selectCountOf().from(RecordCorn.class).count();
        }else {
            return SQLite.selectCountOf().from(RecordCorn.class).where(RecordCorn_Table.uniqueId.eq(uniqueId)).count();
        }
    }


    /**
     * 根据编号获取数据
     * @param uniqueId
     * @return
     */
    public List<RecordCorn> getUniqueIdList(String uniqueId){
        List<RecordCorn> mRecordCornList = null;
        if (!TextUtils.isEmpty(uniqueId)) {
            mRecordCornList = SQLite.select().from(RecordCorn.class).where(
                    RecordCorn_Table.uniqueId.like("%" + uniqueId + "%"),
                    RecordCorn_Table.impurityAverageValue.eq(false),
                    RecordCorn_Table.imperfectGrainValue.eq(false),
                    RecordCorn_Table.densityAverageValue.eq(false)
                    ).queryList();
        }
        return mRecordCornList;
    }
}
